package com.mbbmap.util;

import org.json.simple.JSONArray;
import org.json.simple.JSONValue;

public class StringUtils {
	public static String NullSafe(String str){
		if((str == null) || (str.trim().equalsIgnoreCase("null")))
			str = "";
		else
			str = str.trim();
		return str;
	}
	
	public static String format001(String str){
		
		if(str.length()==1){
			str = str.concat("000");
			str = org.apache.commons.lang.StringUtils.leftPad(str, 5, "0");
		} else {
			
			if(str.indexOf(".") != -1){
			
				int idx = str.indexOf(".");
				String beforeDot = str.substring(0, idx);
				String afterDot  = str.substring(idx+1, str.length());
				
				beforeDot = org.apache.commons.lang.StringUtils.leftPad(beforeDot, 2, "0");
				afterDot  = org.apache.commons.lang.StringUtils.rightPad(afterDot, 3, "0");
				
				str = beforeDot + afterDot;
			
			} else {
				str = org.apache.commons.lang.StringUtils.rightPad(str, 5, "0");
			}
		}
		
		return str;
	}
	
	public static String decryptJSON(String data) {
		String res = "";
		
		if (data != null && !"".equals(data.trim())) {
			JSONArray arr = (JSONArray) JSONValue.parse(data);

			for (int i=0; i<arr.size(); i++) {
				try { 
					
					String var = EncryptionHelper.decrypt(arr.get(i).toString()); 
					arr.set(i, var);

				} catch (Exception e) {}
			}
			
			res = arr.toJSONString();
		}
		
		return res;
	}
}
